package com.onpositive.utils;

import java.util.ArrayList;
import java.util.HashMap;

public class StringPool {	
	
	public final static int MISSING_STRING_INDEX = -1 ;
	
	ArrayList<String> storage = new ArrayList<String>() ;
	HashMap<String,Integer> index = new HashMap<String, Integer>();	
	
	public int putString( String str )
	{
		Integer ind = index.get( str ) ;
		if( ind == null )
		{
			ind = storage.size() ;
			storage.add( str ) ;
			index.put(str, ind ) ;
		}		
		return ind ;
	}
	
	public int getIndex( String str )
	{
		Integer ind = index.get( str ) ;
		ind = ( ind == null || ind < 0 ) ? MISSING_STRING_INDEX : ind ; 
		return ind ;
	}
	
	public String getString( int ind )
	{
		return ind < storage.size() ? storage.get(ind) : null ;
	}
}
